package com.hua.yuan.live.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hua.yuan.live.R;
import com.hua.yuan.live.adapter.RedPackageAdapter;
import com.hua.yuan.util.DensityUtil;

/**
 * 红包弹窗
 */
public class RedRackageDialog extends PopupWindow implements
        AdapterView.OnItemClickListener,
        View.OnClickListener
{

    private int mWidth;

    private int mHeight;

    private int pixWidthDp=16;

    private int pixHeightDp=30;

    private OnRedPackageListener onClickListener;

    private ListView lsvRedPackage;

    private RedPackageAdapter mAdapter;

    private TextView tvCancel;

    private TextView tvMoney;

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if(onClickListener==null){
            return;
        }
        Integer money= (Integer) mAdapter.getItem(position);
        onClickListener.sendPackage(view,money);
    }

    @Override
    public void onClick(View v) {

        if(R.id.tv_cancel_red_package==v.getId()){
            dismiss();
        }
    }

    public interface OnRedPackageListener{

         void sendPackage(View view,Integer money);

    }


    public void setOnRedPackageListener(OnRedPackageListener pOnClickListener){

        this.onClickListener=pOnClickListener;
    }



    public RedRackageDialog(@NonNull Context pContext){

        View view=LayoutInflater.from(pContext).inflate(R.layout.dialog_redpackage_live_room,null);
        tvMoney=view.findViewById(R.id.tv_money_red_package);
        this.setContentView(view);
        mWidth= DensityUtil.dip2px(pContext,131);
        mHeight=DensityUtil.dip2px(pContext,215);
        pixWidthDp=DensityUtil.dip2px(pContext,50);
        pixHeightDp=DensityUtil.dip2px(pContext,40);
        tvCancel=view.findViewById(R.id.tv_cancel_red_package);
        this.setWidth(mWidth);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(mHeight);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        lsvRedPackage=view.findViewById(R.id.lsv_red_package);
        mAdapter=new RedPackageAdapter(pContext);
        lsvRedPackage.setAdapter(mAdapter);
        lsvRedPackage.setOnItemClickListener(this);
        tvCancel.setOnClickListener(this);
    }

    public void setMoney(Context pContext,String pMoney){

        String regx=pContext.getResources().getString(R.string.account_live_room);
        tvMoney.setText(String.format(regx,pMoney));

    }


    public void showUp(View v) {
        //获取需要在其上方显示的控件的位置信息
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        //在控件上方显示
        showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - mWidth / 2-pixWidthDp, location[1] - mHeight+pixHeightDp);
    }
}
